let utils = {};

utils.isMobile = function(val) {
  return val && (/^1[345789]\d{9}$/i.test(val));
}
utils.isEmail = function(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
  // return val && (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i.test(val));
}
utils.isIdCard = function(val) {
  return val && (/^[1-9]\d{5}(19\d{2}|[2-9]\d{3})((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])(\d{4}|\d{3}X)$/i.test(val));
}

utils.isUrl = function(str) {
  let v = new RegExp(
    '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
    'i');
  return v.test(str);
}

let validatorUrl = (rule, value, callback) => {
  if (utils.isUrl(value)) {
    return callback();
  }
  callback(new Error('请输入正确的网址'));
};

const validateRequire = (rule, value, callback) => {
  if (!value) {
    callback(new Error('不能为空'))
  } else {
    callback()
  }
}

const requireValidator = {
  required: true,
  message: '不能为空',
  trigger: 'blur',
  validator: validateRequire
}

let notEmpty = [requireValidator];

let validatorEmail = (rule, value, callback) => {
  if (!value) {
    return callback();
  }
  if (utils.isEmail(value)) {
    return callback();
  }
  callback(new Error('请输入正确的邮箱号码'));
};

let validatorMobile = (rule, value, callback) => {
  if (utils.isMobile(value)) {
    return callback();
  }
  callback(new Error('请输入正确的手机号'));
};
let validatorPhone = (rule, value, callback) => {
  if (!value) {
    return callback();
  }
  if (/^\d{7}$/i.test(value)) {
    return callback();
  }
  callback(new Error('请输入正确的电话号码'));
};
let validatorIdCard = (rule, value, callback) => {
  if (utils.isIdCard(value)) {
    return callback();
  }
  callback(new Error('请输入正确的证件号码'));
};

let validatorPwd = (rule, value, callback) => {
  var pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,99}');
  if (pwdRegex.test(value)) {
    return callback();
  }
  callback(new Error('密码复杂度过低，必须包含字母、数字、特殊字符，至少8个字符'));
};

let formRules = {
  username: notEmpty,
  name: notEmpty,
  realname: notEmpty,
  gender: notEmpty,
  mobile: [{
    required: true,
    trigger: 'blur',
    validator: validatorMobile
  }],
  phone: [{
    trigger: 'blur',
    validator: validatorPhone
  }],
  password: [{
    required: true,
    // message: '不能为空',
    trigger: 'blur',
    validator: validatorPwd
  }],
  idCard: [{
    trigger: 'blur',
    validator: validatorIdCard
  }],
  email: [{
    trigger: 'blur',
    validator: validatorEmail
  }],
}

const mobileValidator = {
  required: true,
  trigger: 'blur',
  validator: validatorMobile
}
const phoneValidator = {
  trigger: 'blur',
  validator: validatorPhone
}
const passwordValidator = {
  trigger: 'blur',
  validator: validatorPwd
}
const idCardValidator = {
  trigger: 'blur',
  validator: validatorIdCard
}
const emailValidator = {
  trigger: 'blur',
  validator: validatorEmail
}
const urlValidator = {
  trigger: 'blur',
  validator: validatorUrl
}

export default formRules
export {
  utils,
  requireValidator,
  mobileValidator,
  phoneValidator,
  passwordValidator,
  idCardValidator,
  emailValidator,
  urlValidator
}

/****************************************
 * usage
 *
 import { requireValidator,
  mobileValidator,
  phoneValidator,
  passwordValidator,
  idCardValidator,
  emailValidator } from '@/utils/rules';

**/
